#include "mainwindow.h"
#include "ui_mainwindow.h"
#include"qpainter.h"
#include <QDebug>
#include <QPen>
#include<QPainter>
#include<iostream>
#include<QInputDialog>
#include<QMessageBox>
#include <sstream>
#include <QString>
#include<cmath>
#include <QPixmap>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    isDrawing=false;
    image=new QImage(this->size(),QImage::Format_RGB32);
    image->fill(Qt::white);
    ui->centralWidget->setMouseTracking(true);
    setMouseTracking(true);
    count=0;
    DrawType=0;
    induimg.load("://pic/inductance.png");
    induimg2.load(":/pic/inductance 2.png");
}

MainWindow::~MainWindow()
{
    delete ui;
}
/*QPoint near(QPoint a)
{
    QPoint b;
   if(a.x()%20!=0)
   {
    if(a.x()%20>=10)
    {
        b.setX(a.x()-a.x()%20+20);
    }
    if(a.x()%20<10)
    {
        b.setX(a.x()-a.x()%20);
    }
   }
   else
   {
       b.setX(a.x());
   }
   if(a.y()%20!=0)
   {
    if(a.y()%20>=10)
    {
        b.setY(a.y()-a.y()%20+20);
    }
    if(a.y()%20<10)
    {
        b.setY(a.y()-a.y()%20);
    }

   }
   else
   {
       b.setY(a.y());
   }
    return b;
}
int max(int a,int b)
{
    if(a>b) return a;
    else return b;

}

int min(int a,int b)
{
    if(a<b) return a;
    else return b;

}
void juzhentemp(double aa[],double bb[],int n)
{  //交换数组指定的两行，即进行行变换（具体为行交换）
    int i;
    double temp1;
    for(i=0; i<n; i++)
    {
        temp1 = aa[i];
        aa[i] = bb[i];
        bb[i] = temp1;
    }
}
*/

void MainWindow::paintEvent(QPaintEvent *)
{
   QPainter cpainter(this);
    cpainter.drawImage(this->rect(),*image,image->rect());

}
bool online(QPoint Point,line L)
{
    if(Point.x()==L.initialpoint.x()&&Point.y()==L.initialpoint.y())
    {
        return 1;
    }
else if(Point.x()==L.endpoint.x()&&Point.y()==L.endpoint.y())
    {
        return 1;
    }
   else if(L.initialpoint.x()==L.endpoint.x())//横线
    {
        if(Point.x()==L.endpoint.x()&&Point.y()<max(L.endpoint.y(),L.initialpoint.y())&&Point.y()>min(L.endpoint.y(),L.initialpoint.y()))
        {
            return 1;
        }
        else {
            return 0;
        }
    }
    else if(L.initialpoint.y()==L.endpoint.y())//竖线
    {
        if(Point.y()==L.endpoint.y()&&Point.x()<max(L.endpoint.x(),L.initialpoint.x())&&Point.x()>min(L.endpoint.x(),L.initialpoint.x()))
        {
            return 1;
        }
        else
        {
          return 0;
        }
    }
   //斜线情况
  else if((Point.x()-L.initialpoint.x())*(L.endpoint.y()-Point.y())==(Point.y()-L.initialpoint.y())*(L.endpoint.x()-Point.x()))//相似三角形判断在不在线上
    {
        return 1;
    }
    else return 0;


}


void MainWindow::mousePressEvent(QMouseEvent *event)
{


  if(DrawType==1)
  {

    count++;
    if(count==1)
    {
    isDrawing=true;
    inipoint=near(event->pos());

                                                                              // cntpa[initialpoint.x()]++;
                                                                                  // cntpb[initialpoint.y()]++;//记录一点为线始终次数
                                                                        //  if(cntpa[initialpoint.x()]==3&&cntpb[initialpoint.y()]==3)//三条线以其为始终时生成结点
                                                                                    //  {
                                                                            //     QPainter dotpainter;
                                                                          //     dotpainter.drawPoint(initialpoint);
                                                                                                       //  }
                                                                     //  if(cntpa[initialpoint.x()]==2&&cntpb[initialpoint.y()]==2)
                                                                                          // {
                                                                                       //    preround=1;//点到有线以其为始终的点时预备生成回路
                                                                                    //}
    L[countline].initialpoint=inipoint;
                                                                          //qDebug()<<L[countline].initialpoint.x();

    }

    if(count==2)
    {
        edpoint=near(event->pos());
        QPainter painter(image);
        painter.drawLine(inipoint,edpoint);
                                                                                            //  cntpa[endpoint.x()]++;
                                                                                    //   cntpb[endpoint.y()]++;
        L[countline].endpoint=edpoint;

        lfer++;//line for each round记录一个回路线数
        countline++;                                                             //     if(cntpa[initialpoint.x()]==3&&cntpb[initialpoint.y()]==3)
                                                                                            //    {
                                                                                         //        QPainter dotpainter;
                                                                       //        dotpainter.drawPoint(initialpoint);
                                                            // qDebug()<<L[countline].endpoint.x();                                                         //    }
                                                                    //  if(cntpa[initialpoint.x()]==2&&cntpb[initialpoint.y()]==2)

    for(int i=0;i<countline-1;i++)
    {
        flag=0;
        if(online(L[countline-1].endpoint,L[i])==1)
          {  flag=1;
            break;
        }
    }
        if(flag==1)
        {
                                                                                    // if(preround==1)
               countround++;                                                                           //{
               for(int i=1;i<=lfer;i++)//生成回路
               {
                                                                                                //r[countround].l[i]=L[countline-i-1];

                      L[countline-i].owner[0]=countround-1;
                      L[countline-i].owner[1]=countround-1;//确定一般支路归属


               }


               for(int i=0;i<countline;i++)
               {
                   if(online(edpoint,L[i])&&online(L[countline-lfer].initialpoint,L[i]))
                   {
                       L[i].owner[1]=countround-1;//给延伸回路的支路标注第二个归属
                   }

               }
               lfer=0;                                                                                     //从第二个回路开始，每个都在原基础上添加，所以先加一
                                                                                             // if(countround==1)  L[0].owner[1]--;                                                                       //}
        }
        isDrawing=false;
        count=0;

    }
   }
/*if(DrawType==2)
  {
    for(int j=0;j<countline;j++)
    {
        qDebug()<<L[j].owner[0]<<' '<<L[j].owner[1];
    }
          isDrawing=true;
         QPainter recpainter(image);
          rcenter=near(event->pos());
       for(int i=0;i<countline;i++)
       {
          if(online(rcenter,L[i]))
          {
              if(L[i].initialpoint.x()==L[i].endpoint.x())
              {
                    QPoint upperleft;
                  upperleft.setX(rcenter.x()-5);
                  upperleft.setY(rcenter.y()-9);
                  QPoint upside;
                  QPoint downside;
                  upside.setX(rcenter.x());
                  upside.setY(rcenter.y()-9);
                  downside.setX(rcenter.x());
                  downside.setY(rcenter.y()+9);
                  QPen wl(Qt::white);
                 recpainter.setPen(wl);
                  recpainter.drawLine(upside,downside);
                  QPen bl(Qt::black);
                 recpainter.setPen(bl);
                  recpainter.drawRect(upperleft.x(),upperleft.y(),10,18);
              }
              if(L[i].initialpoint.y()==L[i].endpoint.y())
              {
                    QPoint upperleft;
                  upperleft.setX(rcenter.x()-9);
                  upperleft.setY(rcenter.y()-5);
                  QPoint leftside;
                  QPoint rightside;
                  leftside.setX(rcenter.x()-9);
                  leftside.setY(rcenter.y());
                  rightside.setX(rcenter.x()+9);
                  rightside.setY(rcenter.y());
                  QPen wl(Qt::white);
                 recpainter.setPen(wl);
                  recpainter.drawLine(leftside,rightside);
                  QPen bl(Qt::black);
                 recpainter.setPen(bl);
                  recpainter.drawRect(upperleft.x(),upperleft.y(),18,10);

              }
          }
       }



                                                                                                                      //a[L[i].owner[0]][L[i].owner[1]]+=L[i].R[countr[i]].rv;



          isDrawing=false;


  }
  if(DrawType==3)
  {
    isDrawing=true;
    QPainter spainter(image);
    QPoint scenter;
    scenter=near(event->pos());
    QPoint upperleft1;
    upperleft1.setX(scenter.x()-10);
    upperleft1.setY(scenter.y()-10);
    spainter.drawEllipse(upperleft1.x(),upperleft1.y(),20,20);
    for(int i=0;i<countline;i++)
    {
        if(online(scenter,L[i]))
        {
            if(L[i].initialpoint.x()==L[i].endpoint.x())//在竖线上
            {
                if(L[i].owner[0]!=L[i].owner[1])
                {
                    spainter.drawText(scenter.x()+12,scenter.y()-8,"-");
                    spainter.drawText(scenter.x()+12,scenter.y()+15,"+");
                }
                else
                {
                    spainter.drawText(scenter.x()+12,scenter.y()-8,"+");
                    spainter.drawText(scenter.x()+12,scenter.y()+15,"-");
                }
            }
            if(L[i].initialpoint.y()==L[i].endpoint.y())//在横线上
            {
                if(L[i].owner[0]!=L[i].owner[1])
                {
                    spainter.drawText(scenter.x()-15,scenter.y()+14,"+");
                    spainter.drawText(scenter.x()+8,scenter.y()+14,"-");
                }
                else
                {
                    spainter.drawText(scenter.x()-15,scenter.y()+14,"-");
                    spainter.drawText(scenter.x()+8,scenter.y()+14,"+");
                }
            }

        }
    }

  }
  if(DrawType==5)//赋电阻值
  {
    for(int i=0;i<15;i++)
    {
        if(online(near(event->pos()),L[i])==1)
        {
            bool ok;
            tempr=QInputDialog::getDouble(this,"input","请输入值",20,0,100,5,&ok);//弹出输入框获取值
            L[i].trv+=tempr;

            if(L[i].initialpoint.x()==L[i].endpoint.x())//在竖线上
           {
            QPainter value(image);
          QString e=QString::number(tempr);
            value.drawText(near(event->pos()).x()+12,near(event->pos()).y()+4,e);
            }
            if(L[i].initialpoint.y()==L[i].endpoint.y())//在横线上
            {
            QPainter value(image);
            QString e=QString::number(tempr);
            value.drawText(near(event->pos()).x()-7,near(event->pos()).y()+20,e);
            }
            tempr=0;
           break;
        }

    }

  }
  if(DrawType==6)//赋电源值
  {
    for(int i=0;i<15;i++)
    {
       if(reactance==0)
       {
           qDebug()<<"ok";
        if(online(near(event->pos()),L[i]))
        {
            bool ok1;

            temps=QInputDialog::getDouble(this,"input","请输入值",20,0,100,5,&ok1);//弹出输入框获取值
            L[i].tsv+=temps;
            if(L[i].initialpoint.x()==L[i].endpoint.x())//在竖线上
           {
            QPainter value(image);
          QString f=QString::number(temps);
            value.drawText(near(event->pos()).x()+12,near(event->pos()).y(),f);
            }
            if(L[i].initialpoint.y()==L[i].endpoint.y())//在横线上
            {
            QPainter value(image);
            QString f=QString::number(temps);
            value.drawText(near(event->pos()).x()-6,near(event->pos()).y()+20,f);
            }
            temps=0;
            break;
        }
       }
       if(reactance==1)
       {
        if(online(near(event->pos()),L[i]))
        {
            bool ok1;

            temps=QInputDialog::getDouble(this,"input","请输入值",20,0,100,5,&ok1);//弹出输入框获取值
            L[i].tsv+=temps;
            tempsj=QInputDialog::getDouble(this,"input","请输入值",20,0,100,5,&ok1);//弹出输入框获取值
            L[i].tsjv+=tempsj;
            if(L[i].initialpoint.x()==L[i].endpoint.x())//在竖线上
            {
            QPainter value(image);
            QString w;
            QString f=QString::number(temps);
            QString s=QString::number(tempsj);
            if(tempsj!=0.0)
            {
            w=f+'+'+s+'j';
            }
            else {
                w=f;
            }
            value.drawText(near(event->pos()).x()+12,near(event->pos()).y(),w);
            }
            if(L[i].initialpoint.y()==L[i].endpoint.y())//在横线上
            {
            QPainter value(image);
            QString w;
            QString f=QString::number(temps);
            QString s=QString::number(tempsj);
            if(tempsj!=0.0)
            {
            w=f+'+'+s+'j';
            }
            else {
                 w=f;
            }
            value.drawText(near(event->pos()).x()-6,near(event->pos()).y()+20,w);

            }
            temps=0;
            break;
        }
       }
    }

  }
  if(DrawType==7)
  {
      isDrawing=true;
     QPainter cappainter(image);
      capcenter=near(event->pos());
      for(int i=0;i<countline;i++)
      {
         if(online(capcenter,L[i]))
         {
             if(L[i].initialpoint.x()==L[i].endpoint.x())//竖线
             {
                 QPen wl(Qt::white);
                cappainter.setPen(wl);
                QPoint up;
                QPoint down;
                up.setX(L[i].initialpoint.x());
                up.setY(capcenter.y()-3);
                down.setX(L[i].initialpoint.x());
                down.setY(capcenter.y()+3);
                cappainter.drawLine(up,down);
                QPoint tempini1;QPoint tempend1;
                tempini1.setX(up.x()-6);tempini1.setY(up.y());
                tempend1.setX(up.x()+6);tempend1.setY(up.y());
                QPoint tempini2;QPoint tempend2;
                tempini2.setX(down.x()-6);tempini2.setY(down.y());
                tempend2.setX(down.x()+6);tempend2.setY(down.y());
                QPen bl(Qt::black);
                cappainter.setPen(bl);
                cappainter.drawLine(tempini1,tempend1);
                cappainter.drawLine(tempini2,tempend2);

             }
             if(L[i].initialpoint.y()==L[i].endpoint.y())//横线
             {
                 QPen wl(Qt::white);
                cappainter.setPen(wl);
                QPoint left;
                QPoint right;
                left.setX(capcenter.x()-3);
                left.setY(capcenter.y());
                right.setX(capcenter.x()+3);
                right.setY(capcenter.y());
                cappainter.drawLine(left,right);
                QPoint tempini1;QPoint tempend1;
                tempini1.setX(left.x());tempini1.setY(left.y()-6);
                tempend1.setX(left.x());tempend1.setY(left.y()+6);
                QPoint tempini2;QPoint tempend2;
                tempini2.setX(right.x());tempini2.setY(right.y()-6);
                tempend2.setX(right.x());tempend2.setY(right.y()+6);
                QPen bl(Qt::black);
                cappainter.setPen(bl);
                cappainter.drawLine(tempini1,tempend1);
                cappainter.drawLine(tempini2,tempend2);
             }
         }
      }


  }
  if(DrawType==8)
  {
      isDrawing=true;
     QPainter indupainter(image);
      inducenter=near(event->pos());
      for(int i=0;i<countline;i++)
      {
         if(online(inducenter,L[i]))
         {
             if(L[i].initialpoint.x()==L[i].endpoint.x())//竖线
             {
                indupainter.drawPixmap(inducenter.x()-8,inducenter.y()-10,16,20,induimg2);

             }
             if(L[i].initialpoint.y()==L[i].endpoint.y())//横线
             {
                indupainter.drawPixmap(inducenter.x()-10,inducenter.y()-8,20,16,induimg);
             }
         }
      }
  }
  if(DrawType==9)
  {
      for(int i=0;i<15;i++)
      {
          if(online(near(event->pos()),L[i])==1)
          {
              bool ok2;
              tempj=QInputDialog::getDouble(this,"input","请输入值",20,0,100,5,&ok2);//弹出输入框获取值
              L[i].tjv+=tempj;

              if(L[i].initialpoint.x()==L[i].endpoint.x())//在竖线上
             {
              QPainter value(image);
              QString e=QString::number(tempj);
              e=e+'j';
              value.drawText(near(event->pos()).x()+12,near(event->pos()).y()+4,e);
              }
              if(L[i].initialpoint.y()==L[i].endpoint.y())//在横线上
              {
              QPainter value(image);
              QString g=QString::number(tempj);
              g=g+'j';
              value.drawText(near(event->pos()).x()-7,near(event->pos()).y()+20,g);
              }
              tempj=0;
             break;
          }

      }
  }
}
void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
    isDrawing=false;
}
void MainWindow::mouseMoveEvent(QMouseEvent *event)
{

    this->update();
}
void MainWindow::mouseclickEvent(QMouseEvent *event)
{

}




void MainWindow::on_pushButton_clicked()
{
    DrawType=1;

}

void MainWindow::on_pushButton_2_clicked()
{
    DrawType=2;
}


void MainWindow::on_pushButton_3_clicked()
{
    DrawType=3;
}


void MainWindow::on_pushButton_4_clicked()
{

      for(int j=0;j<countline;j++)
        {
             if(L[j].owner[0]==L[j].owner[1])
                 {
                 b2[L[j].owner[0]]+=L[j].tsv;
                 b4[L[j].owner[0]]+=L[j].tsjv;

                 }
             else
             {
                 b2[L[j].owner[0]]+=L[j].tsv;
                 b2[L[j].owner[1]]-=L[j].tsv;
                 b4[L[j].owner[0]]+=L[j].tsjv;
                 b4[L[j].owner[1]]-=L[j].tsjv;
             }                                          //a[L[j].owner[0]][L[j].owner[1]]+=L[j].trv; //qDebug()<<a[L[j].owner[0]][L[j].owner[1]];

                                                              //QString c=QString::number(b[i]);
        }

                                                                                // QMessageBox::about(NULL,"ans",c);
        for(int j=0;j<countline;j++)
        {
            a[L[j].owner[0]][L[j].owner[1]]+=L[j].trv;//创建电阻矩阵
            z[L[j].owner[0]][L[j].owner[1]]+=L[j].tjv;//创建电抗矩阵
           if(L[j].owner[0]!=L[j].owner[1])
           {
            for(int i=0;i<countround;i++)
            {
                if(L[j].owner[0]==i||L[j].owner[1]==i)
                {
                    a[i][i]+=L[j].trv;
                    z[i][i]+=L[j].tjv;
                }
            }
           }
         }




        for(int i=0;i<countline;i++)
        {
           for(int j=i;j>=0;j--)
           {
               if(i!=j)
               {
                   a[j][i]=-a[j][i];//非对角线取反
                   a[i][j]=a[j][i];//填充下三角
                   z[j][i]=-z[j][i];//非对角线取反
                   z[i][j]=z[j][i];//填充下三角
               }

           }

        }
                                         //for(int i=0;i<countround;i++)
                                                          //  {
                                                   // for(int j=0;j<countround;j++)
                                                    // {
                                                          // qDebug()<<a[i][j];
                                                      //}
                                                  //}
                                                                    //  for(int i=0;i<countline;i++)
                                                                           // {
                                                                             //    t[i]=a[i][i];
                                                                        //    for(int j=0;j<countround;j++)
                                                                //   {

                                                     //       a[i][i]=a[i][i]+a[i][j];//对角线上的加上（因为前面取反所以是减号）同行其余电阻绝对值
                                                     //
                                                                //       }
                                                               //   a[i][i]=a[i][i]-t[i];
                                                                             // }
                                                            //qDebug()<<L[0].trv<<L[1].trv<<L[2].trv<<L[3].trv<<L[5].trv<<L[6].trv;


                                                               // QString c=QString::number);
                                                            // QMessageBox::about(NULL,"ans",c);

//--------------------------------求电阻矩阵逆
if(reactance==0)
{
int n=countround;
        int i,j,kk=0,k,u;
        double b[n][2*n],c[n][n],yinzhi;
            double bb;
            for(i=0;i<n;i++)  //初始化一个辅助矩阵
                for(j=0;j<2*n;j++)
                    b[i][j]=0;


            //把矩阵a复制给矩阵b
                for(i=0;i<n;i++)
                  for(j=0;j<n;j++)
                     b[i][j]=a[i][j];

               for(j=0;j<n;j++)
                   b[j][n+j]=1;


           for(i=0;i<n;i++)
           {
            // b[i][i] 等于 0 的情况
               if(b[i][i]==0)
               for(j=i;j<n;j++)
               {
                   if(b[j][i]!=0)
                     juzhentemp(b[i],b[j],2*n);  //交换两行
               }
            // b[i][i] 不等于 0 的情况
               for(k=i+1;k<n;k++)
               {
                   yinzhi = -1 * b[k][i] / b[i][i];
                   for(u=0; u < 2*n; u++)
                   {
                      b[k][u] = b[k][u] + b[i][u] * yinzhi;
                   }
              }
           }



                for(i=0; i<n; i++)    //左矩阵 的对角线 全部 转化为  1
                {
                     bb = b[i][i];   //  bb 不会等于0 因为对角线有有一个为0 说明行列式的值为零，
                     for(j=0; j<2*n; j++)
                        b[i][j] = b[i][j] / bb;
                }
                for(i=n-1; i>0; i--)
                for(k=0; k<i; k++)
                {
                    bb = b[k][i];
                    for(u=0; u<2*n; u++)
                        b[k][u] = b[k][u] - bb*b[i][u];
                }
           for(i=0; i<n; i++)
            { for(j=0; j<n; j++)
                {
                   c[i][j] = b[i][j+n]; //qDebug()<<c[i][j];
               }
         }
            kk = 0;

   //-------------------------------------------分割线

            double ans[10]={0};
for(int i=0;i<countround;i++)
{
    for(int j=0;j<countround;j++)
    {
        ans[i]=ans[i]+b2[j]*c[i][j];//矩阵乘法得答案列向量
    }

}

for(int j=0;j<countround;j++)
    {
        //qDebug()<<ans[j];
        QString c=QString::number(ans[j]);
        QString d=QString::number(j+1);
        QMessageBox::about(NULL,d,c);
    }
}
if(reactance!=0)
{
    for(int i=0; i<countround; i++)
     { for(int j=0; j<countround; j++)
         {
           //qDebug()<<z[i][j];
        }
  }
    //-----------------------------------------------------------求电阻矩阵逆
    int n=countround;
            int i,j,kk=0,k,u;
            double b[n][2*n],c[n][n],yinzhi;
                double bb;
                for(i=0;i<n;i++)  //初始化一个辅助矩阵
                    for(j=0;j<2*n;j++)
                        b[i][j]=0;


                //把矩阵a复制给矩阵b
                    for(i=0;i<n;i++)
                      for(j=0;j<n;j++)
                         b[i][j]=a[i][j];

                   for(j=0;j<n;j++)
                       b[j][n+j]=1;


               for(i=0;i<n;i++)
               {
                // b[i][i] 等于 0 的情况
                   if(b[i][i]==0)
                   for(j=i;j<n;j++)
                   {
                       if(b[j][i]!=0)
                         juzhentemp(b[i],b[j],2*n);  //交换两行
                   }
                // b[i][i] 不等于 0 的情况
                   for(k=i+1;k<n;k++)
                   {
                       yinzhi = -1 * b[k][i] / b[i][i];
                       for(u=0; u < 2*n; u++)
                       {
                          b[k][u] = b[k][u] + b[i][u] * yinzhi;
                       }
                  }
               }



                    for(i=0; i<n; i++)    //左矩阵 的对角线 全部 转化为  1
                    {
                         bb = b[i][i];   //  bb 不会等于0 因为对角线有有一个为0 说明行列式的值为零，
                         for(j=0; j<2*n; j++)
                            b[i][j] = b[i][j] / bb;
                    }
                    for(i=n-1; i>0; i--)
                    for(k=0; k<i; k++)
                    {
                        bb = b[k][i];
                        for(u=0; u<2*n; u++)
                            b[k][u] = b[k][u] - bb*b[i][u];
                    }
               for(i=0; i<n; i++)
                { for(j=0; j<n; j++)
                    {
                       c[i][j] = b[i][j+n]; //qDebug()<<c[i][j];
                   }
             }
                kk = 0;
//---------------------------------------------------------------

                double temp1[10][10]={{0}};//-----------BA逆
                for(int i = 0; i < countround; ++i)//矩阵相乘
                    {
                        for(int j = 0; j < countround; ++j)
                        {
                            temp1[i][j]=0;
                            for(int k = 0; k < countround; ++k)
                            temp1[i][j] += z[i][k] * c[k][j];
                            if(temp1[i][j]<0.001)temp1[i][j]=0;

                        }
                    }
                double temp2[10][10]={{0}};//------------BA逆B
                for(int i = 0; i < countround; ++i)//矩阵相乘
                    {
                        for(int j = 0; j < countround; ++j)
                        {
                            temp2[i][j]=0;
                            for(int k = 0; k < countround; ++k)
                            temp2[i][j] += temp1[i][k] * z[k][j];
                        }
                    }
                double temp3[10][10]={{0}};//--------------A+BA逆B
                for(int i = 0; i < countround; ++i)//矩阵相乘
                    {
                        for(int j = 0; j < countround; ++j)
                        {
                            temp3[i][j]=a[i][j]+temp2[i][j];
                        }
                    }
         //--------------------------------------------------求(A+BA逆B)的逆
                int n3=countround;
                        int i3,j3,kk3=0,k3,u3;
                        double b3[n3][2*n3],yinzhi3;
                            double bb3;
                            for(i3=0;i3<n3;i3++)  //初始化一个辅助矩阵
                                for(j3=0;j3<2*n3;j3++)
                                    b3[i3][j3]=0;


                            //把矩阵a复制给矩阵b
                                for(i3=0;i3<n3;i3++)
                                  for(j3=0;j3<n3;j3++)
                                     b3[i3][j3]=temp3[i3][j3];

                               for(j3=0;j3<n3;j3++)
                                   b3[j3][n3+j3]=1;


                           for(i3=0;i3<n3;i3++)
                           {
                            // b[i][i] 等于 0 的情况
                               if(b3[i3][i3]==0)
                               for(j3=i3;j3<n3;j3++)
                               {
                                   if(b3[j3][i3]!=0)
                                     juzhentemp(b3[i3],b3[j3],2*n3);  //交换两行
                               }
                            // b[i][i] 不等于 0 的情况
                               for(k3=i3+1;k3<n3;k3++)
                               {
                                   yinzhi3 = -1 * b3[k3][i3] / b3[i3][i3];
                                   for(u3=0; u3 < 2*n3; u3++)
                                   {
                                      b3[k3][u3] = b3[k3][u3] + b3[i3][u3] * yinzhi3;
                                   }
                              }
                           }



                                for(i3=0; i3<n3; i3++)    //左矩阵 的对角线 全部 转化为  1
                                {
                                     bb3 = b3[i3][i3];   //  bb 不会等于0 因为对角线有有一个为0 说明行列式的值为零，
                                     for(j3=0; j3<2*n3; j3++)
                                        b3[i3][j3] = b3[i3][j3] / bb3;
                                }
                                for(i3=n3-1; i3>0; i3--)
                                for(k3=0; k3<i3; k3++)
                                {
                                    bb3 = b3[k3][i3];
                                    for(u3=0; u3<2*n3; u3++)
                                        b3[k3][u3] = b3[k3][u3] - bb3*b3[i3][u3];
                                }
                           for(i3=0; i3<n3; i3++)
                            { for(j3=0; j3<n3; j3++)
                                {
                                   c3[i3][j3] = b3[i3][j3+n3];
                               }
                         }
                            kk3 = 0;
     //-----------------------------------------------------------
                            double temp4[10][10]={{0}};//-----------A逆B
                            for(int i = 0; i < countround; ++i)//矩阵相乘
                                {
                                    for(int j = 0; j < countround; ++j)
                                    {
                                        temp4[i][j]=0;
                                        for(int k = 0; k < countround; ++k)
                                        temp4[i][j] += c[i][k] * z[k][j];
                                    }
                                }
                                         //-----------A逆B*(A+BA逆B)的逆注意负号
                            for(int i = 0; i < countround; ++i)//矩阵相乘
                                {
                                    for(int j = 0; j < countround; ++j)
                                    {
                                        hou[i][j]=0;
                                        for(int k = 0; k < countround; ++k)
                                        hou[i][j] += temp4[i][k] * c3[k][j];
                                    }
                                }
                            for(int i = 0; i < countround; ++i)//加负号
                                {
                                    for(int j = 0; j < countround; ++j)
                                    {
                                        hou[i][j]=-hou[i][j];qDebug()<<hou[i][j];
                                    }
                                }

                for(int i=0;i<countround;i++)//实部乘实部
                {
                    for(int j=0;j<countround;j++)
                    {
                        ss[i]=ss[i]+b2[j]*c3[i][j];
                    }
                }
                for(int i=0;i<countround;i++)//虚部乘虚部
                {
                    for(int j=0;j<countround;j++)
                    {
                        xx[i]=xx[i]+b4[j]*hou[i][j];
                    }
                }
                for(int i=0;i<countround;i++)//虚部乘虚部取相反数
                {
                        xx[i]=-xx[i];
                }
                for(int i=0;i<countround;i++)//实部乘虚部
                {
                    for(int j=0;j<countround;j++)
                    {
                        sx[i]=sx[i]+b4[j]*c3[i][j];
                    }
                }
                for(int i=0;i<countround;i++)//虚部乘实部
                {
                    for(int j=0;j<countround;j++)
                    {
                        xs[i]=xs[i]+b2[j]*hou[i][j];
                    }
                }
                for(int i=0;i<countround;i++)//加总得最终实虚部
                {
                        fins[i]=ss[i]+xx[i];
                        finx[i]=xs[i]+sx[i];
                }
           for(int i=0;i<countround;i++)
           {

               QString shi=QString::number(fins[i]);
               QString zong;
                QString xu=QString::number(finx[i]);
                QString d=QString::number(i+1);
                if(finx[i]>=0)
                {
                  zong=shi+'+'+xu+'j';
                }
                if(finx[i]<0)
                {
                   zong=shi+xu+'j';

                }
               QMessageBox::about(NULL,d,zong);
           }

}


}


void MainWindow::on_pushButton_5_clicked()
{
    DrawType=5;

}

void MainWindow::on_pushButton_6_clicked()
{
    DrawType=6;
}


void MainWindow::on_pushButton_7_clicked()
{
    DrawType=7;
    reactance=1;
}

void MainWindow::on_pushButton_8_clicked()
{
    DrawType=8;
    reactance=1;
}


void MainWindow::on_pushButton_9_clicked()
{
    DrawType=9;
    reactance=1;
}

void MainWindow::on_pushButton_10_clicked()
{
    clean=1;
    QPainter clean(image);
    QBrush w(Qt::white);
    clean.setBrush(w);

           clean.drawRect(0,0,3000,3000);
     for(int i=0;i<countline;i++)
     {
         L[i].trv=0;
         L[i].tjv=0;
         L[i].tsv=0;
         L[i].tsjv=0;
         L[i].owner[0]=99;
         L[i].owner[1]=99;

     }

     countround=0;
     countline=0;
     count=0;
     lfer=0;
     flag=0;


}

void MainWindow::on_pushButton_11_clicked()
{
    net++;
   if(net%2!=0)
   {
    QPainter npainter(image);
    QPen pen(Qt::green);
    npainter.setPen(pen);
    for(int i=0;i<10000;i=i+20)
        {
           npainter.drawLine(i,0,i,3000);

       }
   for(int i=0;i<10000;i=i+20)
      {
           npainter.drawLine(0,i,3000,i);

     }
   }
   if(net%2==0)
   {
       QPainter npainter(image);
       QPen pen(Qt::white);
       npainter.setPen(pen);
       for(int i=0;i<10000;i=i+20)
          {
              npainter.drawLine(i,0,i,3000);

          }
      for(int i=0;i<10000;i=i+20)
         {
              npainter.drawLine(0,i,3000,i);

        }
      for(int i=0;i<countline;i++)
      {
           QPen pen1(Qt::black);
           npainter.setPen(pen1);
         npainter.drawLine(L[i].initialpoint,L[i].endpoint);
      }
   }

}
*/
void MainWindow::on_pushButton_12_clicked()
{
     image->save("F:mypicture.png");
}
